/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ownPackage;




import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;



public class CopyAndMove {

     // If targetLocation does not exist, it will be created.
    public void copyDirectory(File sourceLocation , File targetLocation) throws FileNotFoundException, IOException {

        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

            String[] children = sourceLocation.list();
            for (int i=0; i<children.length; i++) {

                copyDirectory(new File(sourceLocation, children[i]),
                        new File(targetLocation, children[i]));
            }
        }
        else {

            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetLocation);

            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
    }

    public void moveDirectory(File sourceLocation , File targetLocation)
     throws IOException {
        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

            String[] children = sourceLocation.list();
            for (int i=0; i<children.length; i++) {
                File origin = new File(sourceLocation, children[i]);
                origin.renameTo(new File(targetLocation, origin.getName()));
                System.out.println("name file "+origin.getName());
            }
        }
        else {

            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetLocation);

            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
    }


     public void deleteDirectory(File sourceLocation) {
        if (sourceLocation.isDirectory()) {
            
            String[] children = sourceLocation.list();
            for (int i=0; i<children.length; i++) {
                File origin = new File(sourceLocation, children[i]);
                origin.delete();

            }
        }
    }
   
}


